package com.qdsg.ysg.doctor.nurse.activity;

import android.annotation.SuppressLint;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RatingBar;
import android.widget.ScrollView;
import android.widget.TextView;

import com.qdsg.ysg.doctor.R;
import com.qdsg.ysg.doctor.base.BaseActivity;
import com.qdsg.ysg.doctor.ui.WebViewActivity;
import com.qdsg.ysg.doctor.ui.adapter.AddMedicineAdapter;
import com.qdsg.ysg.doctor.ui.adapter.ExtraListAdapter;
import com.qdsg.ysg.doctor.ui.adapter.NurseInfoAdapter;
import com.qdsg.ysg.doctor.ui.adapter.NurseItemsAdapter;
import com.qdsg.ysg.doctor.ui.adapter.SecondAdapter;
import com.qdsg.ysg.doctor.ui.dialog.MedicineListDialog;
import com.qdsg.ysg.doctor.ui.dialog.SaveDialog;
import com.qdsg.ysg.doctor.util.ToastUtil;
import com.qdsg.ysg.doctor.util.UtilString;
import com.rest.business.RestProxy;
import com.rest.response.PatientProjectDTO;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import androidx.annotation.RequiresApi;
import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.core.widget.NestedScrollView;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;
import jz.joyoung.robot.util.LogUtil;

/**
 * Description
 * Created by lbj on 2022/4/9 15:02
 */
public class AtyHistoryDetail extends BaseActivity {


    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.tv_look_record)
    TextView tvLookRecord;
    @BindView(R.id.tv_person_name)
    TextView tvPersonName;
    @BindView(R.id.tv_person_sex)
    TextView tvPersonSex;
    @BindView(R.id.tv_person_age)
    TextView tvPersonAge;
    @BindView(R.id.tv_person_tel)
    TextView tvPersonTel;
    @BindView(R.id.tv_person_address)
    TextView tvPersonAddress;
    @BindView(R.id.tv_person_time)
    TextView tvPersonTime;
    @BindView(R.id.cl_address)
    ConstraintLayout clAddress;
    @BindView(R.id.rv_info)
    RecyclerView rvInfo;
    @BindView(R.id.rv_items)
    RecyclerView rvItems;
    @BindView(R.id.rv_extra)
    RecyclerView rvExtra;
    @BindView(R.id.tv_extra_price)
    TextView tvExtraPrice;
    @BindView(R.id.ll_extra_price)
    LinearLayout llExtraPrice;
    @BindView(R.id.ratingBar)
    RatingBar ratingBar;
    @BindView(R.id.tv_detail)
    TextView tvDetail;
    @BindView(R.id.nested_scrollView)
    NestedScrollView nestedScrollView;
    @BindView(R.id.tv_rating_bar_title)
    TextView tvRatingBarTitle;
    @BindView(R.id.cl_evaluate)
    ConstraintLayout clEvaluate;
    private MedicineListDialog mMedicineDialog = null;
    private SaveDialog mSaveDialog = null;
    AddMedicineAdapter adapter;
    SecondAdapter secondAdapter;
    List<String> mInfoList = new ArrayList<>();
    List<String> mItemList = new ArrayList<>();
    Map<String, List<PatientProjectDTO.NaAdditionalConsumablesDTO>> mExtraList = new HashMap<>();
    List<String> mKeyList = new ArrayList<>();

    String appointmentId, preAppendId, icdId, tailAppendId, diagnoseId, patientId, oneselfState;
    private NurseInfoAdapter nurseInfoAdapter;
    private NurseItemsAdapter nurseItemsAdapter;
    private ExtraListAdapter extraListAdapter;


    @Override
    protected int getLayoutResource() {
        return R.layout.aty_history_detail;
    }

    @Override
    protected void initData() {
        appointmentId = getIntent().getStringExtra( "appointmentId" );
        if (!UtilString.isEmpty( appointmentId )) {
            appointmentDetail();
        }


    }


    @Override
    protected void initViews() {
        tvTitle.setText( "订单详情" );
        LinearLayoutManager managerInfo = new LinearLayoutManager( mContext );
        managerInfo.setOrientation( LinearLayoutManager.VERTICAL );
        rvInfo.setLayoutManager( managerInfo );
        LinearLayoutManager manager = new LinearLayoutManager( mContext );
        manager.setOrientation( LinearLayoutManager.VERTICAL );
        rvItems.setLayoutManager( manager );
        LinearLayoutManager managerExtra = new LinearLayoutManager( mContext );
        managerExtra.setOrientation( LinearLayoutManager.VERTICAL );
        rvExtra.setLayoutManager( managerExtra );
        nurseInfoAdapter = new NurseInfoAdapter( mContext, mInfoList );
        rvInfo.setAdapter( nurseInfoAdapter );
        nurseItemsAdapter = new NurseItemsAdapter( mContext, mItemList );
        rvItems.setAdapter( nurseItemsAdapter );
        extraListAdapter = new ExtraListAdapter( mContext, mExtraList, mKeyList, false );
        rvExtra.setAdapter( extraListAdapter );

    }

    @OnClick({R.id.tv_look_record})
    public void onViewClicked(View view) {
        if (view.getId() == R.id.tv_look_record) {
            Bundle bundleHistory = new Bundle();
            bundleHistory.putString( "patientId", patientId );
            bundleHistory.putString( "fromWhere", "CloudListActivity" );
            startActivity( WebViewActivity.class, bundleHistory );
        }
    }



    private void appointmentDetail() {
        RestProxy.getInstance().appointmentDetail( appointmentId, new Observer<PatientProjectDTO>() {
            @Override
            public void onSubscribe(Disposable d) {

            }

            @RequiresApi(api = Build.VERSION_CODES.N)
            @SuppressLint("SetTextI18n")
            @Override
            public void onNext(PatientProjectDTO patientProjectDTO) {
                try {
                    patientId = patientProjectDTO.data.patientId;
                    tvPersonName.setText( "姓名：" + patientProjectDTO.data.patientName );
                    tvPersonSex.setText( "性别：" + patientProjectDTO.data.sex );
                    tvPersonAge.setText( "年龄：" + patientProjectDTO.data.age + "岁" );
                    tvPersonAddress.setText( "地址：" + patientProjectDTO.data.adress );
                    tvPersonTime.setText( "预约时间：" + patientProjectDTO.data.appointmentTime );
                    tvPersonTime.setText( "联系方式：" + patientProjectDTO.data.patientPhone );
                    tvExtraPrice.setText( patientProjectDTO.data.orderAmount );
                    mInfoList.clear();
                    if ("1".equals( patientProjectDTO.data.isEyeRed )) {
                        mInfoList.add( "眼睛是否红、肿、痛：是" );
                    } else {
                        mInfoList.add( "眼睛是否红、肿、痛：否" );
                    }
                    if ("1".equals( patientProjectDTO.data.isSecretion )) {
                        mInfoList.add( "有无分泌物：有分泌物" );
                    } else {
                        mInfoList.add( "有无分泌物：无分泌物" );
                    }
                    if ("1".equals( patientProjectDTO.data.isTrichiasis )) {
                        mInfoList.add( "睫毛有无倒睫：有倒睫" );
                    } else {
                        mInfoList.add( "睫毛有无倒睫：无倒睫" );
                    }
                    if ("1".equals( patientProjectDTO.data.isUnobstructed )) {
                        mInfoList.add( "泪道是否通畅：通畅" );
                    } else {
                        mInfoList.add( "泪道是否通畅：不通畅" );
                    }
                    if ("1".equals( patientProjectDTO.data.isSuture )) {
                        mInfoList.add( "眼部是否有缝线：有缝线" );
                        mInfoList.add( "缝线位置：" + patientProjectDTO.data.suturePosition );
                        if ("1".equals( patientProjectDTO.data.isRemove )) {
                            mInfoList.add( "是否需要拆线：需要" );
                        } else {
                            mInfoList.add( "是否需要拆线：不需要" );
                        }

                    } else {
                        mInfoList.add( "眼部是否有缝线：无缝线" );
                    }
                    nurseInfoAdapter.notifyDataSetChanged();

                    mItemList.clear();
                    mItemList.add( patientProjectDTO.data.projectTitle );
                    nurseItemsAdapter.notifyDataSetChanged();

                    if (UtilString.isEmpty( patientProjectDTO.data.commentContent ) || UtilString.isEmpty( patientProjectDTO.data.commentScore )) {
                        clEvaluate.setVisibility( View.GONE );
                    }
                    if (UtilString.isEmpty( patientProjectDTO.data.commentContent )) {
                        tvDetail.setVisibility( View.GONE );
                    } else {
                        tvDetail.setText( patientProjectDTO.data.commentContent );
                    }
                    if (UtilString.isEmpty( patientProjectDTO.data.commentScore )) {
                        ratingBar.setVisibility( View.GONE );
                    } else {
                        ratingBar.setRating( Float.parseFloat( patientProjectDTO.data.commentScore ) );
                    }
                    //耗材信息
                    mExtraList.clear();
                    mKeyList.clear();
                    List<PatientProjectDTO.NaAdditionalConsumablesDTO> consumeMaterials = patientProjectDTO.data.consumeMaterials;
                    Map<String, List<PatientProjectDTO.NaAdditionalConsumablesDTO>> collect = consumeMaterials.stream().collect( Collectors.groupingBy( PatientProjectDTO.NaAdditionalConsumablesDTO::getOrderId ) );
                    mExtraList.putAll( collect );
                    for (Map.Entry<String, List<PatientProjectDTO.NaAdditionalConsumablesDTO>> entry : collect.entrySet()) {
                        String key = entry.getKey();
                        mKeyList.add( key );
                    }
                    LogUtil.e( "耗材--" + mKeyList.size() );
                    LogUtil.e( "耗材集合--" + mExtraList.size() );
                    extraListAdapter.notifyDataSetChanged();


                } catch (Exception e) {
                    e.printStackTrace();
                }

            }

            @Override
            public void onError(Throwable e) {
                ToastUtil.onError( mContext, e );
            }

            @Override
            public void onComplete() {

            }
        } );

    }


}
